package com.lmf.tool;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.lmf.entity.Area;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.codehaus.jackson.map.ObjectMapper;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

public class HttpUtil {
    public static final String URL="http://gateway.iems.net.cn/GsmsHttp";
    public static final String USERNAME="72135:admin";
    public static final String PASSWORD="26390810";
    /**
     * get方法
     */
    public static void getRequest(String mobile,String content) throws IOException {
        //创建一个默认的链接
        CloseableHttpClient closeableHttpClient= HttpClients.createDefault();
        //创建一个请求
        String url=URL+"?username="+USERNAME+"&password="+PASSWORD+"&to="+mobile+"&content="+content;
        HttpGet httpGet=new HttpGet(url);
        //执行请求获取响应的结果
        CloseableHttpResponse response=closeableHttpClient.execute(httpGet);
        //获取响应的状态码
        int status=response.getStatusLine().getStatusCode();
        System.out.println(status);
        System.out.println(response.getEntity().getContent());
        response.close();
        closeableHttpClient.close();
    }
    /**
     * post方法
     */
    public static void postRequest() throws IOException {
        //创建一个默认的链接
        CloseableHttpClient closeableHttpClient= HttpClients.createDefault();
        HttpPost httpPost=new HttpPost("https://wms.wljkxys.com/v2/Purchase/getList");
        //设置请求头
        httpPost.setHeader("token","ee4f7b439fbc911694f3952c644941cc");
        //创建请求参数
        List<NameValuePair> pairList=new ArrayList<>();
        pairList.add(new BasicNameValuePair("genre","1"));
        //创建请求体,封装参数
        UrlEncodedFormEntity encodedFormEntity=new UrlEncodedFormEntity(pairList,"UTF-8");
        //将请求体交给当前请求
        httpPost.setEntity(encodedFormEntity);
        CloseableHttpResponse response=closeableHttpClient.execute(httpPost);
       // int responseStatus=response.getStatusLine().getStatusCode();
        HttpEntity httpEntity=response.getEntity();
        String result= EntityUtils.toString(httpEntity,"UTF-8");
        JSONObject jsonObject= JSON.parseObject(result);
        String data=jsonObject.getString("data");
        DataDto dataDto= JSON.parseObject(data, DataDto.class);
        List<Area> list=dataDto.getData();
        list.forEach(System.out::println);
        closeableHttpClient.close();
    }

}
